package com.gz.mylife.app;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gz.mylife.Define;
import com.gz.mylife.R;
import com.gz.mylife.widget.ElasticGridView;
import com.gz.mylife.widget.FooterView;
import com.gz.mylife.widget.FooterView.OnFooterViewRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.zl.smartmall.library.IGZAddress;
import com.zl.smartmall.library.po.SearchHotWord;
import com.zl.smartmall.library.po.SearchRecord;
import com.zl.smartmall.library.po.ShoppingCartInfo;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.CrashHandler;
import com.zl.smartmall.library.util.SingtonAsyncHttpClient;
import com.zl.smartmall.library.util.Tools;
import com.zl.smartmall.library.vo.SearchRecordDao;

public class ShoppingSearchActivity extends BaseActivity implements OnClickListener,OnFooterViewRefreshListener,OnItemClickListener{
	private static final String TAG = ShoppingSearchActivity.class.getSimpleName();
	
	private static final int REQ_FOR_RED_WORD = 1;
	private static final int REQ_SEARCH_FIRST_PAGE = 2;
	private static final int REQ_SEARCH_MORE_PAGE = 3;
	
	
	private EditText mSearchContent;
	private ImageView mSearchDelete;
	private ImageView mSearchGo;
	private LinearLayout mSearchBack;
	
	private AsyncHttpClient mHttpClient;
	private HttpHandler mHttpHandler;
	
	private LayoutInflater mInflate;
	private PullToRefreshListView mRecordList;
	private HistorySearchRecordAdapter mRecordAdapter;
	private View mHeaderView;
	private View mHeaderTop;
	private TextView mhvRecordTitle;
//	private View mSearchRecordFooterView;
	private ElasticGridView mGridview;
	private MyGridViewAdapter mHotWordAdapter;
	private PullToRefreshListView mSearchList;
	private SearchResultAdapter mSearchAdapter;
	private FooterView mFooterView;
	private Dialog mProgressDialog;	
	
	private List<SearchHotWord> mHotWordDataList;
	private List<SearchRecord> mRecordDataList;
	private List<ShoppingCartInfo> mSearchRecordList;
	private SearchRecordDao mSearchRecordDao;
	private DisplayImageOptions options;
	
	private int mReqCode = Define.REQ_UNKNOW;
	private String mContent;
	private int page = 1;
	
	private MyHandler mHandler;
	
	private class MyHandler extends Handler{
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case REQ_FOR_RED_WORD:
				//刷新mRecordList的头部
				mHeaderTop.setVisibility(View.VISIBLE);
				mHotWordAdapter.notifyDataSetChanged();
				break;
			case REQ_SEARCH_FIRST_PAGE:
				mRecordList.setVisibility(View.GONE);
				mSearchList.setVisibility(View.VISIBLE);
				mSearchAdapter.notifyDataSetChanged();
				break;
			case REQ_SEARCH_MORE_PAGE:
//				mRecordList.setVisibility(View.GONE);
//				mSearchList.setVisibility(View.VISIBLE);
				mSearchAdapter.notifyDataSetChanged();
				break;

			default:
				break;
			}
		}
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_shopping_search);
		init();
		initView();
		initListener();
		mSearchList.setVisibility(View.GONE);
		if(mReqCode == Define.REQ_UNKNOW){
			mReqCode = REQ_FOR_RED_WORD;
			loadDataFromServer();
		}
	}
	
	@SuppressLint("NewApi")
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
//		  MenuInflater inflater = getMenuInflater(); 
//		    inflater.inflate(R.menu.shopping_search_menu, menu); 
//		    // 获取SearchView对象 
//		    SearchView searchView = (SearchView) menu.findItem(R.id.shopping_menu_search).getActionView();
//		    if(searchView == null){  
//		    	CrashHandler.d(TAG, "searchview is null");
//		        return true; 
//		    } 
//		    searchView.setIconifiedByDefault(true); 
//		    // 获取搜索服务管理器  
//		    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
//		    // searchable activity的component name，由此系统可通过intent进行唤起
//		    ComponentName cn = new ComponentName(this,SearchResultActvity.class); 
//		    // 通过搜索管理器，从searchable activity中获取相关搜索信息，就是searchable的xml设置。如果返回null，表示该activity不存在，或者不是searchable
//		    SearchableInfo info = searchManager.getSearchableInfo(cn); 
//		    if(info == null){ 
//		    	
//		    }      
//		    // 将searchable activity的搜索信息与search view关联
//		    searchView.setSearchableInfo(info); 

		
		
		    return true; 
	}
	
	private void init() {
		mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);  
		mActionBar.setCustomView(R.layout.shopping_search_trigger_layout);//自定义ActionBar布局
		
		mHttpClient = SingtonAsyncHttpClient.getInstance();
		mHttpHandler = new HttpHandler(this);
		
		mHotWordDataList = new ArrayList<SearchHotWord>();
		mHandler = new MyHandler();
		mInflate = getLayoutInflater();
		
		mRecordDataList = new ArrayList<SearchRecord>();
		mSearchRecordDao = new SearchRecordDao(getApplicationContext());
		List<SearchRecord> mTempRecordList  = mSearchRecordDao.getRecordList();
		if(mTempRecordList != null){
			mRecordDataList.clear();
			mRecordDataList.addAll(mTempRecordList);
		}
		
		mSearchRecordList = new ArrayList<ShoppingCartInfo>();
		options = buildOptions();
	}
	private DisplayImageOptions buildOptions() {
		return new DisplayImageOptions.Builder()
        .resetViewBeforeLoading()
        .bitmapConfig(Bitmap.Config.ARGB_8888)
//        .cacheInMemory()
        .cacheOnDisc()
        .showStubImage(R.drawable.picture_frame)
        .showImageOnFail(R.drawable.picture_frame)
        .imageScaleType(ImageScaleType.IN_SAMPLE_INT) // default
        .displayer(new RoundedBitmapDisplayer(5))
        .build();
	}
	private void checkSearchRecordIsNull(){
		if(mRecordDataList.size()>0){
//			mRecordList.removeFooterView(mSearchRecordFooterView);
			mhvRecordTitle.setText("搜索记录：");
		}else{
//			mRecordList.addFooterView(mSearchRecordFooterView);
			mhvRecordTitle.setText("您还没有搜索记录");
		}
	}

	private void initView() {
		mSearchContent = (EditText) findViewById(R.id.et_search_content);
		mSearchDelete = (ImageView) findViewById(R.id.iv_search_delete);
		mSearchDelete.setVisibility(View.GONE);
		mSearchGo = (ImageView) findViewById(R.id.iv_search_trigger);
		mSearchGo.setOnClickListener(this);
		mSearchBack = (LinearLayout) findViewById(R.id.layout_actionbar_up);
		mRecordList = (PullToRefreshListView) findViewById(R.id.list_record);
		mSearchList = (PullToRefreshListView) findViewById(R.id.list_search);
		mSearchList.setOnItemClickListener(this);
		mSearchAdapter = new SearchResultAdapter();
		mFooterView = (FooterView) mInflate.inflate(R.layout.footer_view, null);
		mSearchList.getRefreshableView().addFooterView(mFooterView);
		mSearchList.setMode(Mode.DISABLED);
		mFooterView.setOnFooterViewRefreshListener(this);
		mSearchList.setOnScrollListener(mFooterView);
		mSearchList.setAdapter(mSearchAdapter);
		mRecordAdapter = new HistorySearchRecordAdapter();
		mRecordList.setMode(Mode.DISABLED);
		mRecordList.setOnItemClickListener(this);
		mHeaderView = mInflate.inflate(R.layout.shopping_search_header_layout, null);
		mRecordList.getRefreshableView().addHeaderView(mHeaderView);
		mRecordList.setAdapter(mRecordAdapter);
		mHeaderTop = mHeaderView.findViewById(R.id.rl_top);
		mhvRecordTitle = (TextView) mHeaderView.findViewById(R.id.tv_search_record_title);
		mGridview = (ElasticGridView) mHeaderView.findViewById(R.id.gv_hot_word);
		//mGridview添加适配器
		mHotWordAdapter = new MyGridViewAdapter();
		mGridview.setAdapter(mHotWordAdapter);
		mGridview.setOnItemClickListener(this);
//		mSearchRecordFooterView = mInflate.inflate(R.layout.shopping_search_footer, null);
		checkSearchRecordIsNull();
		mHeaderTop.setVisibility(View.GONE);
		mProgressDialog = new Dialog(this, R.style.DialogTheme);
		View view = View.inflate(this, R.layout.loading_data_dialog, null);
		mProgressDialog.setContentView(view);
		mProgressDialog.setCanceledOnTouchOutside(false);
	}

	private void initListener() {
		mSearchContent.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				
			}
			
			@Override
			public void afterTextChanged(Editable arg0) {
				if(TextUtils.isEmpty(mSearchContent.getText().toString())){
					mSearchDelete.setVisibility(View.GONE);
				}else{
					mSearchDelete.setVisibility(View.VISIBLE);
				}
			}
		});
		mSearchBack.setOnClickListener(this);
		mSearchDelete.setOnClickListener(this);
		mSearchBack.setOnTouchListener(myTouchLister);
	}
	private View.OnTouchListener myTouchLister = new View.OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				v.setBackgroundResource(R.drawable.bg_actionbar_menu_pressed);
				break;
			case MotionEvent.ACTION_CANCEL:
			case MotionEvent.ACTION_UP:
				v.setBackgroundResource(R.color.transparent);
				break;
			}
			return false;
		}
	};
	
	public void loadDataFromServer(){
		if(!Tools.hasInternetConnection(getApplicationContext())){
			switch (mReqCode) {
			case REQ_FOR_RED_WORD:
				mHeaderTop.setVisibility(View.GONE);
				break;
			case REQ_SEARCH_FIRST_PAGE:
				mProgressDialog.dismiss();
				Toast.makeText(getApplicationContext(), Define.MSG_NO_NETWORK, 0).show();
				break;
			case REQ_SEARCH_MORE_PAGE:
				mFooterView.onRefreshSuccess(Define.MSG_NULL_CONNECT);
				break;

			default:
				break;
			}
			mReqCode = Define.REQ_UNKNOW;
			return;
		}
		String url = Constants.URL_SHOPPING_SEARCH;//"http://192.168.103.74/health/api/mall/products/searchs";//Define.URL_SHOPPING_SEARCH;
		
		RequestParams params = new RequestParams();
		params.put("cmid", IGZAddress.getInstance().getCurrentCommunityID());
		switch (mReqCode) {
		case REQ_FOR_RED_WORD:
			break;
		case REQ_SEARCH_FIRST_PAGE:
			params.put("keywords", mContent);
			params.put("page", page+"");
			page++;
			break;
		case REQ_SEARCH_MORE_PAGE:
			params.put("keywords", mContent);
			params.put("page", page+"");
			page++;
			break;

		default:
			break;
		}
		
		mHttpClient.post(url, params, mHttpHandler);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.layout_actionbar_up:
			finish();
			break;
		case R.id.iv_search_delete:
			mSearchContent.setText("");
			break;
		case R.id.iv_search_trigger:
			String content = mSearchContent.getText().toString();
			if(!TextUtils.isEmpty(content)){
				SearchRecord record = new SearchRecord(content,1);
				if(!mRecordDataList.contains(record)){
					mSearchRecordDao.insert(record);
					mRecordDataList.add(0, record);
				}else {
					mRecordDataList.remove(record);
					mSearchRecordDao.insert(record);
					mRecordDataList.add(0, record);
				}
				mRecordAdapter.notifyDataSetChanged();
				checkSearchRecordIsNull();
				if(mReqCode == Define.REQ_UNKNOW){
					mReqCode = REQ_SEARCH_FIRST_PAGE;
					mContent = content;
					page = 1;
					mSearchRecordList.clear();
					loadDataFromServer();
				}
			}
			break;

		default:
			break;
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		
	}
	private static class HttpHandler extends JsonHttpResponseHandler{
		private ShoppingSearchActivity mOuter;
		
		private int mReqStatus = Define.REQ_UNKNOW;
		private String mReqMsg;
		
		public HttpHandler(ShoppingSearchActivity outer){
			mOuter = outer;
		}
		public void release(){
			new WeakReference<ShoppingSearchActivity>(mOuter);
		}
		@Override
		public void onSuccess(int statusCode, Header[] headers,
				JSONObject response) {
			CrashHandler.d(TAG, "response:"+response);
			if(mOuter == null){
				return;
			}
			try {
				mReqStatus = response.getInt("ret");
				mReqMsg = response.getString("msg");
				JSONArray result = response.getJSONArray("result");
				switch (mOuter.mReqCode) {
				case REQ_FOR_RED_WORD:
					mOuter.parseGetHotWord(result);
					break;
				case REQ_SEARCH_FIRST_PAGE:
					mOuter.parseSearchResult(result);
					break;
				case REQ_SEARCH_MORE_PAGE:
					mOuter.parseSearchResult(result);
					break;

				default:
					break;
				}
				onFinishSelf();
			} catch (JSONException e) {
				e.printStackTrace();
				onFailure(statusCode, headers, "", e);
			}
		
		}
		@Override
		public void onFailure(int statusCode, Header[] headers,
				String responseString, Throwable throwable) {
			if(mOuter == null){
				return;
			}
			switch (mOuter.mReqCode) {
			case REQ_FOR_RED_WORD:
				break;
			case REQ_SEARCH_FIRST_PAGE:
				mOuter.mProgressDialog.dismiss();
				mOuter.page--;
				break;
			case REQ_SEARCH_MORE_PAGE:
				mOuter.page--;
				mOuter.mFooterView.onRefreshSuccess(Define.MSG_LOAD_FAILED_LOAD_AGAIN);
				break;

			default:
				break;
			}
			
			onFinishSelf();
		}
		
		public void onFinishSelf() {
			if(mOuter == null){
				return;
			}
			switch (mOuter.mReqCode) {
			case REQ_FOR_RED_WORD:
				switch (mReqStatus) {
				case 0:
					mOuter.mHandler.sendEmptyMessage(REQ_FOR_RED_WORD);
					break;
				case 4:
					mOuter.mHeaderTop.setVisibility(View.GONE);
					break;

				default:
					break;
				}
				
				break;
			case REQ_SEARCH_FIRST_PAGE:
				//计算fooder怎么显示
				switch (mReqStatus) {
				case 0:
					mOuter.mFooterView.onRefreshSuccess(Define.MSG_LOADING_MORE);
					mOuter.mHandler.sendEmptyMessage(REQ_SEARCH_FIRST_PAGE);
					mOuter.mFooterView.setClickable(true);
					break;
				case 4:
					mOuter.mFooterView.onRefreshSuccess(Define.MSG_SEARCH_NOTHING);
					mOuter.mHandler.sendEmptyMessage(REQ_SEARCH_FIRST_PAGE);
					mOuter.mFooterView.setClickable(false);
					break;

				default:
					break;
				}
				mOuter.mProgressDialog.dismiss();
				break;
			case REQ_SEARCH_MORE_PAGE:
				switch (mReqStatus) {
				case 0:
					mOuter.mHandler.sendEmptyMessage(REQ_SEARCH_MORE_PAGE);
					mOuter.mFooterView.onRefreshSuccess(Define.MSG_LOADING_MORE);
					mOuter.mFooterView.setClickable(true);
					break;
				case 4:
					mOuter.mFooterView.onRefreshSuccess(Define.MSG_NO_MORE_DATA);
					mOuter.mFooterView.setClickable(false);
					break;

				default:
					break;
				}
				break;

			default:
				break;
			}
			mOuter.mReqCode = Define.REQ_UNKNOW;
		}
	}
	
	private void parseGetHotWord(JSONArray result) throws JSONException{
		for(int i=0;i<result.length();i++){
			JSONObject record = result.getJSONObject(i);
			SearchHotWord word = new SearchHotWord();
			word.setKeyWord(record.getString(Params.KEYWORD));
			word.setTimes(record.getInt(Params.TIMES));
			mHotWordDataList.add(word);
		}
		
	}
	
	private void parseSearchResult(JSONArray result) throws JSONException{
		for(int i=0;i<result.length();i++){
			JSONObject record = result.getJSONObject(i);
			ShoppingCartInfo info = new ShoppingCartInfo();
			
			info.setShopId(record.getInt(Params.SHOPID));
			info.setProductId(record.getInt(Params.PRODUCTID));
			info.setProductTitle(record.getString(Params.PRODUCTTITLE));
			info.setPic(record.getString(Params.PIC));
			info.setWidth(record.getInt(Params.WIDTH));
			info.setHeight(record.getInt(Params.HEIGHT));
			info.setTagPrice(record.getDouble(Params.TAGPRICE));
			info.setSalePrice(record.getDouble(Params.SALEPRICE));
			info.setNum(record.getInt(Params.NUM));
			info.setBuyNum(record.getInt(Params.SALENUM));
			info.setShopName(record.getString(Params.SHOPNAME));
			mSearchRecordList.add(info);
		}
	}
	
	class Params{
		public static final String KEYWORD = "keyword";
		public static final String TIMES = "times";
		
		public static final String SHOPID = "shop_id";
		public static final String PRODUCTID = "product_id";
		public static final String PRODUCTTITLE = "product_title";
		public static final String PIC = "pic";
		public static final String WIDTH = "width";
		public static final String HEIGHT = "height";
		public static final String TAGPRICE = "tag_price";
		public static final String SALEPRICE = "sale_price";
		public static final String NUM = "num";
		public static final String SALENUM = "sale_num";
		public static final String SHOPNAME = "shop_name";
	}
	
	private class MyGridViewAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			int size = mHotWordDataList.size();
			return size>5?5:size;
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			ViewHolder holder;
			if(convertView == null){
				convertView = mInflate.inflate(R.layout.shopping_search_hot_word_item, null);
				holder = new ViewHolder();
				holder.keyWord = (TextView) convertView.findViewById(R.id.tv_keyword);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			holder.keyWord.setText(mHotWordDataList.get(position).getKeyWord());
			return convertView;
		}
		
		private class ViewHolder{
			public TextView keyWord;
		}
		
	}
	private class HistorySearchRecordAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return mRecordDataList.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			ViewHolder holder;
			if(convertView == null){
				convertView = mInflate.inflate(R.layout.shopping_search_record_item, null);
				holder = new ViewHolder();
				holder.name = (TextView) convertView.findViewById(R.id.tv_name);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
				holder.name.setText(mRecordDataList.get(position).getKeyWord());
			return convertView;
		}
		private class ViewHolder{
			TextView name;
		}
		
	}
	
	private class SearchResultAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return mSearchRecordList.size();
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			ViewHolderResult holder;
			if(convertView == null){
				holder = new ViewHolderResult();
				convertView = mInflate.inflate(R.layout.shopping_search_result_item, null);
				holder.pic = (ImageView) convertView.findViewById(R.id.iv_product_pic);
				holder.title = (TextView) convertView.findViewById(R.id.tv_product_title);
				holder.saleNum = (TextView) convertView.findViewById(R.id.tv_product_sale_num);
				holder.salePrice = (TextView) convertView.findViewById(R.id.tv_product_price);
				holder.tagPrice = (TextView) convertView.findViewById(R.id.tv_product_tag_price);
				holder.shop = (TextView) convertView.findViewById(R.id.tv_product_shop);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolderResult) convertView.getTag();
			}
			ShoppingCartInfo bean = mSearchRecordList.get(position);
			holder.title.setText(bean.getProductTitle());
			holder.saleNum.setText(bean.getBuyNum()+"");
			holder.salePrice.setText(bean.getSalePrice()+"");
			holder.tagPrice.setText("原价：￥"+bean.getTagPrice()+"");
			holder.tagPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
			holder.shop.setText(bean.getShopName());
			
			if(bean.getPic()!=null){
				ImageLoader.getInstance().displayImage(bean.getPic(), holder.pic, options);
			}
			return convertView;
		}
		
		class ViewHolderResult{
			public ImageView pic;
			public TextView title;
			public TextView saleNum;
			public TextView salePrice;
			public TextView tagPrice;
			public TextView shop;
		}
	}
//	@Override
//	public void onFooterViewClick() {
//		if(mReqCode == Define.REQ_UNKNOW){
//			mReqCode = REQ_SEARCH_MORE_PAGE;
//			mFooterView.startLoading();
//			loadDataFromServer();
//		}
//	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if(parent == mGridview){
			String keyWord = mHotWordDataList.get((int)id).getKeyWord();
			CrashHandler.d(TAG, "key:"+keyWord);
			if(mReqCode == Define.REQ_UNKNOW){
				mReqCode = REQ_SEARCH_FIRST_PAGE;
				mContent = keyWord;
				page = 1;
				mSearchRecordList.clear();
				mProgressDialog.show();
				loadDataFromServer();
			}
		}else if(mRecordList.getVisibility() == View.VISIBLE){
			String keyWord = mRecordDataList.get((int)id).getKeyWord();
			SearchRecord record = new SearchRecord(keyWord,1);
			if(!mRecordDataList.contains(record)){
				mSearchRecordDao.insert(record);
				mRecordDataList.add(0, record);
			}else {
				mRecordDataList.remove(record);
				mSearchRecordDao.insert(record);
				mRecordDataList.add(0, record);
			}
			mRecordAdapter.notifyDataSetChanged();
			if(mReqCode == Define.REQ_UNKNOW){
				mReqCode = REQ_SEARCH_FIRST_PAGE;
				mContent = keyWord;
				page = 1;
				mSearchRecordList.clear();
				mProgressDialog.show();
				loadDataFromServer();
			}
		}else if(mSearchList.getVisibility() == View.VISIBLE){
			if(view == mFooterView){
				return;
			}
			Intent intent = new Intent(getApplicationContext(), ProductDetail.class);
			intent.putExtra("productId", mSearchRecordList.get((int)id).getProductId());
			intent.putExtra("shopId", mSearchRecordList.get((int)id).getShopId());
			startActivity(intent);
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
//		Tools.recordBehavious(getApplicationContext(), TAG, Define.BEHAVIOUS_RESUME);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
//		Tools.recordBehavious(getApplicationContext(), TAG, Define.BEHAVIOUS_PAUSE);
	}

	@Override
	public void onFooterViewRefresh() {
		// TODO Auto-generated method stub
		if(mReqCode == Define.REQ_UNKNOW){
			mReqCode = REQ_SEARCH_MORE_PAGE;
//			mFooterView.start;
			loadDataFromServer();
		}
	}
}
